package com.zzw.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzw.reggie.common.CustomException;
import com.zzw.reggie.entity.Category;
import com.zzw.reggie.entity.Dish;
import com.zzw.reggie.entity.Setmeal;
import com.zzw.reggie.mapper.CategoryMapper;
import com.zzw.reggie.service.ICategoryService;
import com.zzw.reggie.service.IDishService;
import com.zzw.reggie.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private IDishService dishService;

    @Autowired
    private ISetmealService setmealService;

    @Override
    public void remove(Long id) {

        //查询当前分类是否关联了菜品 如果关联 抛出一个业务异常
        //添加查询条件 根据分类id进行查询
        LambdaQueryWrapper<Dish> dlqw = new LambdaQueryWrapper<>();
        dlqw.eq(Dish::getCategoryId,id);
        long count1 = dishService.count(dlqw);
        if (count1 > 0){
            //抛出一个业务异常
            throw new CustomException("当前分类下关联了菜品 不允许删除");
        }

        //查询当前分类是否关联了套餐 如果关联 抛出一个业务异常
        //添加查询条件 根据分类id进行查询
        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
        slqw.eq(Setmeal::getCategoryId,id);
        long count2 = setmealService.count(slqw);
        if (count2 > 0){
            //抛出一个业务异常
            throw new CustomException("当前分类下关联了套餐 不允许删除");
        }

        //条件都不成立 正常删除分类
        super.removeById(id);
    }
}
